package com.example.demo.utils;

import com.example.demo.pojo.BigStar;
import com.example.demo.pojo.Star;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static Star createProxy(BigStar bigStar) {
        Star starProxy = (Star)Proxy.newProxyInstance(
                ProxyUtil.class.getClassLoader(),
                new Class[]{Star.class},
                (proxy, method, args) -> {
                    if(method.getName().equals("sing")) {
                        System.out.println("准备话筒，收钱");
                        return method.invoke(bigStar, args);
                    }
                    else if(method.getName().equals("dance")) {
                        System.out.println("准备场地，收钱");
                        return method.invoke(bigStar, args);
                    }
                    return method.invoke(bigStar, args);
                });
        return starProxy;
    }
}
